package com.e3.basdoc.dept.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.dept.model.DeptConstant;
import com.e3.basdoc.dept.model.DeptFields;
import com.e3.basdoc.dept.model.DeptModel;
import com.e3.basdoc.region.client.RegionRefDialog;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.core.model.ModelHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.data.ModelComparer;
import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class DeptHelper extends TGFHelper<DeptModel>{
	
	public static DeptHelper INSTANCE = new DeptHelper();

	private ModelHelper<DeptModel> deptHelper = new ModelHelper<DeptModel>();
	
	private String[] notNullFields = {DeptModel.CODE,DeptModel.NAME,DeptModel.SHORT_NAME};
	
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private DeptHelper(){
		lengthMap.put(DeptModel.ID, 19);
		lengthMap.put(DeptModel.CODE, 20);
		lengthMap.put(DeptModel.NAME, 50);
		lengthMap.put(DeptModel.SHORT_NAME, 20);
		lengthMap.put(DeptModel.CONTACT_ADDRESS, 100);
		lengthMap.put(DeptModel.EMAIL, 50);
		lengthMap.put(DeptModel.CONTACT_PERSON, 50);
		lengthMap.put(DeptModel.CONTACT_WAY, 100);
		lengthMap.put(DeptModel.FAX, 20);
		lengthMap.put(DeptModel.MANAGER, 20);
		lengthMap.put(DeptModel.OFFICE_ADDRESS, 100);
		lengthMap.put(DeptModel.TELPHONE, 20);
		lengthMap.put(DeptModel.ZIPCODE, 20);
		lengthMap.put(DeptModel.REMARK, 100);
	}


	public Field<?> createFormField(String name) {
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		
		if (name.equals(DeptFields.CODE) || name.equals(DeptFields.CONTACT_ADDRESS) || name.equals(DeptFields.CONTACT_PERSON)
				|| name.equals(DeptFields.CONTACT_WAY) || name.equals(DeptFields.EMAIL) || name.equals(DeptFields.FAX)
				|| name.equals(DeptFields.MANAGER) || name.equals(DeptFields.NAME) || name.equals(DeptFields.OFFICE_ADDRESS)
				|| name.equals(DeptFields.SHORT_NAME) || name.equals(DeptFields.TELPHONE) || name.equals(DeptFields.ZIPCODE)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createStringField(name, dispName,length,isNotNull);
		}
		else if (name.equals(DeptFields.PARENT_DEPT)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new DeptRefDialog(),isNotNull);
		}
		else if (name.equals(DeptFields.REGION)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new RegionRefDialog(),isNotNull);
		}
		else if (name.equals(DeptFields.TYPE)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			// 类型：普通部门(1)、采购部门（2）、销售部门（3）
			listValueModels.add(new ListTypeModel<Integer>(DeptConstant.TYPE_COMMON, DeptFieldsConstant.INSTANCE.type_common()));
			listValueModels.add(new ListTypeModel<Integer>(DeptConstant.TYPE_PURCHASE, DeptFieldsConstant.INSTANCE.type_purchase()));
			listValueModels.add(new ListTypeModel<Integer>(DeptConstant.TYPE_SALES, DeptFieldsConstant.INSTANCE.type_sales()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(DeptFields.FOUND_TIME)) {
			formField = MyFormFieldFactory.createDateField(name, dispName,isNotNull);
		}
		else if (name.equals(DeptFields.REMARK)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,length,isNotNull);
		}
		else if (name.equals(DeptFields.STATUS)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			listValueModels.add(new ListTypeModel<Integer>(DeptConstant.STATUS_NORMAL, DeptFieldsConstant.INSTANCE.status_normal()));
			listValueModels.add(new ListTypeModel<Integer>(DeptConstant.STATUS_REVOCATORY, DeptFieldsConstant.INSTANCE.status_revocatory()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(DeptFields.ID)) {
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}

	/**
	 * 设置地区之间的父子关系。
	 * 
	 * @param pDept
	 * @param children
	 */
	public void setDeptRelations(DeptModel pDept, List<DeptModel> children) {
		if(children==null||children.isEmpty()){
			return;
		}
		for (DeptModel deptModel : children) {
			deptModel.setParentDept(pDept);
		}
	}

	/**
	 * tree和grid利用该比较器实现dept的find和update。
	 * 
	 * @return
	 */
	public ModelComparer<DeptModel> getModelComparer() {
		return deptHelper.getModelComparer();
	}

	public ModelKeyProvider<DeptModel> getModelKeyProvider() {
		return deptHelper.getModelKeyProvider();
	}

	public List<ColumnConfig> getDeptGridColumnConfigs() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = DeptModel.CODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = DeptModel.NAME;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = DeptModel.CONTACT_ADDRESS;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = DeptModel.ZIPCODE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		return configs;
	}

	public String getFieldDisplayName(String code) {
		return DeptFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String getCodeFieldName() {
		return DeptModel.CODE;
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public DeptModel createModelInstance() {
		return new DeptModel();
	}

}
